#include <stdio.h>
#include <string.h>

#include "device_control_data.h"
#include "fast_conble_func.h"
#include "json_data.h"
#include "cJSON.h"
#include "log.h"

// 灯的控制与集合
uint8_t *lightControl_name[] = {"CF_OnOff", "CF_Bright", "CF_ColorTemp"};
#define LIGHTCONTROL_NAME_SIZE sizeof(lightControl_name) / sizeof(lightControl_name[0])

/**
 * @description: 灯控制开关
 * @param {uint16_t} addr 灯短地址
 * @param {uint8_t} Onoff 开或关
 * @return {*}
 */
uint8_t light_control_OnOff(uint16_t addr, uint8_t OnOffState)
{
    uint8_t data[1] = {0};
    log_debug("%s called\n", __func__);

    if (OnOffState)
    {
        data[0] = 0x80;
    }
    else
    {
        data[0] = 0x00;
    }
    FastConbleShortAddrControl(addr, data, sizeof(data));
    return 0;
}

/**
 * @description: 装填灯状态数据进json数据包
 * @param {uint8_t} *data 灯的状态数据（不含前缀）
 * @param {uint16_t} dataLen 数据长度
 * @param {cJSON} *json json数据包
 * @return {*}
 */
int light_status_json(uint8_t *data, uint16_t dataLen, cJSON *json)
{
    char value[10] = {0}; // 用于转换数字
    LIGHT_DATA_t ligthData = {0};
    if (data[0] > 0)
    {
        ligthData.switch_power = 1;
    }
    else
    {
        ligthData.switch_power == 0;
    }

    // 解析协议
    ligthData.Bri = data[0] & 0x7f;                 // 亮度, 由灯具的1-127转为1-100
    ligthData.Bri = (ligthData.Bri + 1) * 100 >> 7; // 测试过

    ligthData.blue = data[1];
    ligthData.red = data[2];
    ligthData.green = data[3];
    ligthData.warm = data[4];
    ligthData.cool = data[5];

    for (uint16_t i = 0; i < dataLen + 1; i++)
    {
        printf("%02X ", *(data + i));
    }
    printf("]\n");

    cJSON *element;
    cJSON_ArrayForEach(element, json) // element是含一个键值对的对象
    {
        cJSON *child = element->child; // 键值对

        while (child != NULL) // 对象内多个键值对问题
        {
            if (!cJSON_IsString(child))
            {
                ; // 不是字符型，跳过
            }
            else
            {
                for (int i = 0; i < LIGHTCONTROL_NAME_SIZE; i++)
                {
                    if (!strcmp(child->string, lightControl_name[i]))
                    {
                        // 都是数值型数据
                        switch (i) // lightControl_name数组
                        {
                        case 0: // CF_OnOff开关
                            // cJSON_setReplaceNumberValue(element, child, ligthData.switch_power);
                            memset(value, 0, sizeof(value));
                            snprintf(value, sizeof(value), "%d", ligthData.switch_power);
                            cJSON_SetValuestring(child, value);
                            break;
                        case 1: // CF_Bright亮度
                            memset(value, 0, sizeof(value));
                            snprintf(value, sizeof(value), "%d", ligthData.Bri);
                            cJSON_SetValuestring(child, value);
                            break;
                        case 2: // CF_ColorTemp色温
                            memset(value, 0, sizeof(value));
                            snprintf(value, sizeof(value), "%d", ligthData.cool);
                            cJSON_SetValuestring(child, value);
                        default:
                            break;
                        }
                    }
                }
            }
            child = child->next;
        }
    }
}

/**
 * @description: 灯控制亮度，这里会重置色温
 * @param {uint16_t} addr
 * @param {uint8_t} Bright
 * @return {*}
 */
uint8_t light_control_Bright(uint16_t addr, uint8_t Bright)
{
    uint8_t data[6] = {0, 0, 0, 0, 0x7f, 0x7f};
    data[0] = (uint16_t)Bright * 127 / 100;
    data[0] |= 0x80;
    printf("%d\n", data[0]);
    FastConbleShortAddrControl(addr, data, sizeof(data));
    return 0;
}

/**
 * @description: 灯控制亮度和色温，同时控制
 * @param {uint16_t} addr 地址
 * @param {uint8_t} Bright 亮度0-100
 * @param {uint8_t} cold 冷光0-255
 * @param {uint8_t} warm 暖光 0-255， cold+warm=255
 * @return {*}
 */
uint8_t light_control_Bright_ColorTemp(uint16_t addr, uint8_t Bright, uint8_t warm, uint8_t cold)
{
    uint8_t data[6] = {0, 0, 0, 0, 0x7f, 0x7f};

    if (cold + warm != 255)
    {
        return 1;
    }
    data[0] = (uint16_t)Bright * 127 / 100;
    data[0] |= 0x80;
    data[4] = warm;
    data[5] = cold;
    printf("%d\n", data[0]);
    FastConbleShortAddrControl(addr, data, sizeof(data));
    return 0;
}


// 灯的控制与集合
uint8_t *switchControl_name[] = {"CF_OnOff"};
#define SWITCHCONTROL_NAME_SIZE sizeof(switchControl_name) / sizeof(switchControl_name[0])
// 开关设备
uint8_t switch_control_OnOff(uint16_t addr, uint8_t OnOffState)
{
    uint8_t data[2] = {0xff, OnOffState};
    FastConbleShortAddrControl(addr, data, sizeof(data));
}

int switch_status_json(uint8_t *data, uint16_t dataLen, cJSON *json)
{
    SWITCH_DATA_t switchData = {0};

    switchData.ctrl_mask = data[0];
    switchData.output_mask = data[1];
    // 单开关
    // 只有一个属性
    cJSON *element;
    cJSON_ArrayForEach(element, json) // element是含一个键值对的对象
    {
        cJSON *child = element->child; // 键值对

        for (int i = 0; i < SWITCHCONTROL_NAME_SIZE; i++)
        {
            if (!strcmp(child->string, switchControl_name[i]))
            {
                // 都是数值型数据
                switch (i) // attributeName_report数组
                {
                case 0: // CF_OnOff开关
                    cJSON_setReplaceNumberValue(element, child, switchData.output_mask);
                    break;
                default:
                    break;
                }
            }
        }
    }
    return 0;
}